package com.shiyu.springssmemployeegit.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shiyu.springssmemployeegit.mapper.EmployeeMapper;
import com.shiyu.springssmemployeegit.pojo.Employee;
import com.shiyu.springssmemployeegit.service.EmployeeService;
import com.shiyu.springssmemployeegit.utils.PageDto;
import com.shiyu.springssmemployeegit.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee findEmployeeById(Long id) {
        Employee i = employeeMapper.selectByPrimaryKey(id);
        System.out.println("i = " + i);
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean insertEmployee(Employee employee) {
        return employeeMapper.insertSelective(employee) > 0;
    }

    @Override
    public boolean updateEmployee(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee) > 0;
    }

    @Override
    public boolean deleteEmployee(Long id) {
        return employeeMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public PageVo pageList(PageDto pageDto) {
        //使用pageHelper开启分页
        Page<Object> page = PageHelper.startPage(pageDto.getCurrent(), pageDto.getPageSize());
        //调用mapper方法
        List<Employee> list = employeeMapper.pageList(pageDto);
        //获取总记录数
        long total = page.getTotal();
        //创建pageVo对象
        PageVo pageVo = new PageVo(total, list);
        return pageVo;
    }


}
